﻿using Face8280WebSocketServer.Command;
using Face8280WebSocketServer.Command.Record;
using Face8280WebSocketServer.Controllers;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FaceFace8280WebSocketServer.Controllers.New.FaceDevice
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceReportNewController : ControllerBase
    {
        /// <summary>
        /// 获取设备出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetReport")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public async Task<IActionResult> GetReport()
        {
            //GetRecord cmd = new GetRecord();

            var first = Allocator.WebSocketClients.FirstOrDefault();
            var detail = await Allocator.GetCommandDetail(first.Value);
            GetRecord cmd = new GetRecord(detail, Face8280WebSocketServer.Command.Models.RecordType.Enter, 10);
            await Allocator.AddCommandAsync(cmd);
            var result = cmd.getResult();
            return new JsonResult(new JsonResultModel(result));
        }

        /// <summary>
        /// 清空出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("ClearReport")]
        [Authorize]
        public async Task<IActionResult> ClearReport()
        {
            var first = Allocator.WebSocketClients.FirstOrDefault();
            var detail = await Allocator.GetCommandDetail(first.Value);
            ClearRecord cmd = new ClearRecord(detail, Face8280WebSocketServer.Command.Models.RecordType.Door);
            await Allocator.AddCommandAsync(cmd);
            var result = cmd.getResult();
            return new JsonResult(new JsonResultModel(result));
        }

        /// <summary>
        /// 获取设备出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("RereadReport")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public async Task<IActionResult> RereadReport()
        {
            //GetRecord cmd = new GetRecord();

            var first = Allocator.WebSocketClients.FirstOrDefault();
            var detail = await Allocator.GetCommandDetail(first.Value);
            RereadRecord cmd = new RereadRecord(detail, Face8280WebSocketServer.Command.Models.RecordType.Door);
            await Allocator.AddCommandAsync(cmd);
            var result = cmd.getResult();
            return new JsonResult(new JsonResultModel(result));
        }
    }
}
